import { notification } from 'antd/lib/index';
import {queryList,addModule,updateModule,moduleDetail,deleModule} from '../services/modularInfo';

export default {
  namespace: 'module',
  state: {
    entityPageResult: {list: []},
    entityInfoObj: {},
  },

  effects: {
    *queryEntityPageList({ payload }, { call, put }) {
      const response = yield call(queryList, payload);

      const entityPageResult = {
        list: response.data,
      };
      yield put({
        type: 'changeEntityPageList',
        payload: { code: response.code, entityPageResult },
      });
    },

    *detailEntity({ payload }, { call, put }) {
      const response = yield call(moduleDetail, payload);

      if (response.code === 10000) {
        yield put({
          type: 'changeDetailEntity',
          payload: response.data,
        });
      }
    },

    *updateEntity({ payload, callback }, { call }) {
      let response;
      if (payload.id) {
        response = yield call(updateModule, payload);
      } else {
        response = yield call(addModule, payload);
      }

      if (response.code === 10000) {
        notification.success({
          message: '数据提交成功!',
        });
        if (callback) callback(response);
      }
    },

    *removeEntity({ payload, callback }, { call }) {
      const response = yield call(deleModule, payload);
      if (response.code === 10000) {
        notification.success({
          message: '删除成功！',
        });
        if (callback) callback(response);
      }
    },
  },

  reducers: {
    changeEntityPageList(state, { payload }) {
      return {
        ...state,
        entityPageResult: payload.entityPageResult,
      };
    },
    changeDetailEntity(state, { payload }) {
      return {
        ...state,
        entityInfoObj: payload,
      };
    },
  },
};
